# Contributor: Marian Buschsieweke <marian.buschsieweke@ovgu.de>
# Maintainer: Bart Ribbers <bribbers@disroot.org>
pkgname=opencv
pkgver=4.8.1
pkgrel=0
pkgdesc="Open source computer vision and machine learning library"
url="https://opencv.org/"
# s390x blocked by vtk -> netcdf
# riscv64: ftbfs, relocation errors
# ppc64le: ftbfs alpine/aports#15494
arch="all !s390x !riscv64 !ppc64le"
license="BSD-3-Clause"
depends_dev="
	blas-dev
	eigen-dev
	ffmpeg-dev
	freetype-dev
	glew-dev
	gst-plugins-base-dev
	gstreamer-dev
	harfbuzz-dev
	hdf5-dev
	libdc1394-dev
	libgphoto2-dev
	libva-glx-dev
	mesa-dev
	onetbb-dev
	openblas-dev
	openexr-dev
	openjpeg-dev
	openjpeg-tools
	py3-setuptools
	qt6-qtbase-dev
	vtk-dev
	"
makedepends="
	$depends_dev
	clang
	cmake
	lld
	samurai
	python3-dev
	py3-gpep517
	py3-numpy-dev
	py3-setuptools
	py3-wheel
	"
subpackages="
	$pkgname-dbg
	$pkgname-dev
	$pkgname-doc
	py3-$pkgname-pyc
	py3-$pkgname:_py3
	libopencv_aruco:_lib
	libopencv_calib3d:_lib
	libopencv_core:_lib
	libopencv_dnn:_lib
	libopencv_face:_lib
	libopencv_features2d:_lib
	libopencv_flann:_lib
	libopencv_highgui:_lib
	libopencv_imgcodecs:_lib
	libopencv_imgproc:_lib
	libopencv_ml:_lib
	libopencv_objdetect:_lib
	libopencv_optflow:_lib
	libopencv_photo:_lib
	libopencv_plot:_lib
	libopencv_shape:_lib
	libopencv_stitching:_lib
	libopencv_superres:_lib
	libopencv_tracking:_lib
	libopencv_video:_lib
	libopencv_videoio:_lib
	libopencv_videostab:_lib
	libopencv_ximgproc:_lib
	"
source="https://github.com/opencv/opencv/archive/$pkgver/opencv-$pkgver.tar.gz
	https://github.com/opencv/opencv_contrib/archive/$pkgver/opencv_contrib-$pkgver.tar.gz
	"
# Tests require human interaction
# net required to download a data file (face_landmark_model.dat)
options="net !check"

prepare() {
	default_prepare

	cd "$srcdir/opencv_contrib-$pkgver"/modules
	# Only use modules that we care about
	mv \
		aruco \
		face \
		tracking \
		optflow \
		plot \
		shape \
		superres \
		videostab \
		ximgproc \
		"$builddir"/modules/
}

build() {
	case "$CARCH" in
	x86_64)
		local extra_cmake_flags="-DCPU_BASELINE_DISABLE=SSE3 -DCPU_BASELINE_REQUIRE=SSE2"
		;;
	esac

	case "$CARCH" in
	riscv64)
		local lto=OFF
		;;
	*)
		local lto=ON
		;;
	esac

	# reduce size of debug syms
	CFLAGS="$CFLAGS -g1" CXXFLAGS="$CXXFLAGS -g1" \
	LDFLAGS="$LDFLAGS -fuse-ld=lld" \
	CC=clang CXX=clang++ \
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DENABLE_BUILD_HARDENING=ON \
		-DENABLE_THIN_LTO=$lto \
		-DWITH_OPENCL=ON \
		-DWITH_OPENGL=ON \
		-DWITH_TBB=ON \
		-DWITH_VULKAN=ON \
		-DWITH_QT=ON \
		-DWITH_ADE=OFF \
		-DWITH_opencv_gapi=OFF \
		-DWITH_IPP=OFF \
		-DBUILD_WITH_DEBUG_INFO=ON \
		-DBUILD_TESTS=OFF \
		-DBUILD_PERF_TESTS=OFF \
		-DBUILD_EXAMPLES=OFF \
		-DINSTALL_C_EXAMPLES=OFF \
		-DINSTALL_PYTHON_EXAMPLES=OFF \
		-DOPENCV_SKIP_PYTHON_LOADER=OFF \
		-DOPENCV_GENERATE_PKGCONFIG=ON \
		-DOPENCV_ENABLE_NONFREE=OFF \
		-DOPENCV_GENERATE_SETUPVARS=OFF \
		-DEIGEN_INCLUDE_PATH=/usr/include/eigen3 \
		-DCMAKE_SKIP_INSTALL_RPATH=ON \
		-DPYTHON_EXECUTABLE=/usr/bin/python3 \
		$extra_cmake_flags
	cmake --build build

	cd modules/python/package
	gpep517 build-wheel \
		--wheel-dir "$builddir"/.dist \
		--output-fd 3 3>&1 >&2
}

check() {
	cd build
	CTEST_OUTPUT_ON_FAILURE=TRUE ctest
}

package() {
	python3 -m installer -d "$pkgdir" \
		.dist/*.whl
	DESTDIR="$pkgdir" cmake --install build
	python3 -m compileall -fq "$pkgdir"/usr/lib/python*

	# opencv's python metainfo names it "opencv", as expected.
	# however, people usually don't use this, they use
	# https://github.com/opencv/opencv-python
	# which is installed with "pip install opencv-python", is
	# exactly the same thing, and is named differently.
	# so, to be satisfied with a system dependency, it has to follow
	# that same name.
	# we can trick pip into thinking both are installed by just copying the
	# dist-info and changing the name.
	cd "$pkgdir"/usr/lib/python*/site-packages
	cp -r opencv-*.dist-info opencv-python-$pkgver.dist-info
	sed -i \
		-e "s|Name: opencv|Name: opencv-python|" \
		opencv-python-$pkgver.dist-info/METADATA
}

samples() {
	pkgdesc="OpenCV Samples"
	depends="$pkgname=$pkgver-r$pkgrel"
	amove usr/share/opencv4/samples
}

_py3() {
	pkgdesc="$pkgdesc (python modules)"
	depends="py3-numpy"

	amove usr/lib/python3.*
}

_lib() {
	pkgdesc="$pkgdesc ($subpkgname library)"

	amove usr/lib/$subpkgname.so.*
}

sha512sums="
b98d89b8e7b8ae8138bce00c5226816b761b53fbeb8f28ca516e08c5d130f216f9388a81785cd6684034530f768e097cbe12f19a9361f362b7d2048bfc427a65  opencv-4.8.1.tar.gz
e3df49e6a1411f55eebbc02f4534ddefabff961b4f63c69b21fe06ff3df894773a8643ab0cb123b83c2bdc8fa02698b332d3c243e8546c894e6c6c8ecaa65500  opencv_contrib-4.8.1.tar.gz
"
